package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallCollect;
import org.linlinjava.litemall.db.mapper.LitemallCollectDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallCollectService {
    @Resource
    private LitemallCollectDao collectDao;

    public int count(int uid, Integer gid) {
    	LitemallCollect collect = new LitemallCollect();
    	collect.setUserId(uid);
    	collect.setValueId(gid);
    	collect.setDeleted(false);
    	return (int)collectDao.templateCount(collect);
		/*
		 * LitemallCollectExample example = new LitemallCollectExample();
		 * example.or().andUserIdEqualTo(uid).andValueIdEqualTo(gid).andDeletedEqualTo(
		 * false); return (int) collectMapper.countByExample(example);
		 */
    }

    public List<LitemallCollect> queryByType(Integer userId, Byte type, Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallCollect> query = collectDao.createLambdaQuery();

        if (type != null) {
        	query.andEq(LitemallCollect::getType, type);
           // criteria.andTypeEqualTo(type);
        }
        
        query.andEq(LitemallCollect::getUserId, userId);
        query.andEq(LitemallCollect::getDeleted, false);
        
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
        query.limit(page, limit);
        
        List<LitemallCollect> list = query.select();
        return list;
    	
		/*
		 * LitemallCollectExample example = new LitemallCollectExample();
		 * LitemallCollectExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (type != null) { criteria.andTypeEqualTo(type); }
		 * criteria.andUserIdEqualTo(userId); criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, limit); return
		 * collectMapper.selectByExample(example);
		 */
    }

    public int countByType(Integer userId, Byte type) {
    	
    	LitemallCollect collect = new LitemallCollect();
    	collect.setUserId(userId);
    	collect.setType(type);
    	collect.setDeleted(false);
    	return (int)collectDao.templateCount(collect);
		/*
		 * LitemallCollectExample example = new LitemallCollectExample();
		 * example.or().andUserIdEqualTo(userId).andTypeEqualTo(type).andDeletedEqualTo(
		 * false); return (int) collectMapper.countByExample(example);
		 */
    }

    public LitemallCollect queryByTypeAndValue(Integer userId, Byte type, Integer valueId) {
    	
    	LitemallCollect collect = new LitemallCollect();
    	collect.setUserId(userId);
    	collect.setType(type);
    	collect.setValueId(valueId);
    	collect.setDeleted(false);
    	return collectDao.templateOne(collect);
    	
		/*
		 * LitemallCollectExample example = new LitemallCollectExample();
		 * example.or().andUserIdEqualTo(userId).andValueIdEqualTo(valueId).
		 * andTypeEqualTo(type).andDeletedEqualTo(false); return
		 * collectMapper.selectOneByExample(example);
		 */
    }

    public void deleteById(Integer id) {
    	
    	LitemallCollect collect = new LitemallCollect();
    	collect.setId(id);
    	collect.setDeleted(true);
    	collectDao.updateTemplateById(collect);
    	
        //collectMapper.logicalDeleteByPrimaryKey(id);
    }

    public int add(LitemallCollect collect) {
        collect.setAddTime(LocalDateTime.now());
        collect.setUpdateTime(LocalDateTime.now());
        collectDao.insertTemplate(collect,true);
        return 1;
        //return collectMapper.insertSelective(collect);
    }

    public List<LitemallCollect> querySelective(String userId, String valueId, Integer page, Integer size, String sort, String order) {
    	
    	LambdaQuery<LitemallCollect> query = collectDao.createLambdaQuery();
    	 if (!StringUtils.isEmpty(userId)) {
    		 query.andEq(LitemallCollect::getUserId, Integer.valueOf(userId));
         }
         if (!StringUtils.isEmpty(valueId)) {
        	 query.andEq(LitemallCollect::getValueId, Integer.valueOf(valueId));
         }
         query.andEq(LitemallCollect::getDeleted, false);
         
         if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	 query.orderBy(sort + " " + order);
         }
       
        query.limit(page, size);
        
        List<LitemallCollect> list = query.select();
        return list;
    	
    	
		/*
		 * LitemallCollectExample example = new LitemallCollectExample();
		 * LitemallCollectExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(userId)) {
		 * criteria.andUserIdEqualTo(Integer.valueOf(userId)); } if
		 * (!StringUtils.isEmpty(valueId)) {
		 * criteria.andValueIdEqualTo(Integer.valueOf(valueId)); }
		 * criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, size); return
		 * collectMapper.selectByExample(example);
		 */
    }
}
